cmake_minimum_required(VERSION 3.5)
project(PLG)

if(NOT DIM)
  set(DIM "2")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-pragmas -DDIM=${DIM}")
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG")
set(CMAKE_CXX_STANDARD 14)

option(DBGLEVEL "The verbosity. " -1)
if (DBGLEVEL GREATER -1)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDBGLEVEL=${DBGLEVEL}")
endif()

option(USE_OPENMP "Whether to use the OpenMP parallelism." OFF)
if (USE_OPENMP)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_OPENMP -fopenmp")
endif()

option(PROFILE "Whether to profile the code. " OFF)
if (PROFILE)
  set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -pg")
endif()

message(STATUS "System : ${CMAKE_SYSTEM_NAME}")
message(STATUS "Build type : ${CMAKE_BUILD_TYPE}")
message(STATUS "CMAKE_CXX_FLAGS : ${CMAKE_CXX_FLAGS}")

option(USE_MKL "Whether to use the Intel(R) MKL. " OFF)
option(USE_OPENBLAS "Whether to use the OpenBLAS. " OFF)
option(USE_OPENBLAS "Whether to use the OpenBLAS. " OFF)
if(USE_MKL)
  if(NOT DEFINED MKLROOT)
    if(NOT DEFINED ENV{MKLROOT})
      message(SEND_ERROR "Please set the MKLROOT variable. ")
    endif()
    set(MKLROOT $ENV{MKLROOT})
  endif()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_MKL -m64 -I${MKLROOT}/include")
elseif(USE_AOCL)
  if(NOT DEFINED AOCL_ROOT)
    if(NOT DEFINED ENV{AOCL_ROOT})
      message(SEND_ERROR "Please set the AOCL_ROOT variable. ")
    endif()
    set(AOCL_ROOT $ENV{AOCL_ROOT})
  endif()
#  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_AOCL -I${AOCL_ROOT}/include")
elseif(USE_OPENBLAS)
  find_package(openblas CONFIG REQUIRED)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUSE_OPENBLAS")
else()
  find_package(PkgConfig REQUIRED)
  pkg_check_modules(BLAS blas)
  pkg_check_modules(CBLAS cblas)
  if(NOT ${BLAS_FOUND} AND NOT ${CBLAS_FOUND})
    message(SEND_ERROR "No BLAS library found. ")
  endif()
  pkg_check_modules(LAPACK REQUIRED lapack)
  pkg_check_modules(LAPACKE REQUIRED lapacke)
endif()

set(MAIN_LIB katfd)

enable_testing()
add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(example)
